package com.future.mayi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.utils.statusbarlibrary.StatusBarUtils;
import com.future.baselib.view.CircleImageView;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.LevelModel;
import com.future.mayi.response.LevelResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.SharedPreferencesUtils;

import java.util.HashMap;

import butterknife.BindView;

public class LevelActivity extends BaseTitleActivity {
    @BindView(R.id.iv_head_img)
    CircleImageView ivHeadImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_star_1)
    ImageView ivStar1;
    @BindView(R.id.pb_2)
    ProgressBar pb2;
    @BindView(R.id.iv_star_2)
    ImageView ivStar2;
    @BindView(R.id.pb_3)
    ProgressBar pb3;
    @BindView(R.id.iv_star_3)
    ImageView ivStar3;
    @BindView(R.id.pb_4)
    ProgressBar pb4;
    @BindView(R.id.iv_star_4)
    ImageView ivStar4;
    @BindView(R.id.tv_star_1)
    TextView tvStar1;
    @BindView(R.id.tv_star_2)
    TextView tvStar2;
    @BindView(R.id.tv_star_3)
    TextView tvStar3;
    @BindView(R.id.tv_star_4)
    TextView tvStar4;
    @BindView(R.id.tv_star_2_hint)
    TextView tvStar2Hint;
    @BindView(R.id.tv_star_2_reward)
    TextView tvStar2Reward;
    @BindView(R.id.tv_star_3_hint)
    TextView tvStar3Hint;
    @BindView(R.id.tv_star_3_reward)
    TextView tvStar3Reward;
    @BindView(R.id.tv_star_4_hint)
    TextView tvStar4Hint;
    @BindView(R.id.tv_star_4_reward)
    TextView tvStar4Reward;

    private ProgressBar progressBar;
    private LevelModel model;

    @Override
    protected void initTopBar() {
        StatusBarUtils.setStatusBarColor(getWindow(), getResources().getColor(R.color.bg_F8F8F8), false);
        rlMainTitle.setBackgroundColor(getResources().getColor(R.color.bg_F8F8F8));
        setViewLineVisibility(View.GONE);
        initBack();
        getTitleText().setText("团长中心");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_level);

    }

    @Override
    protected void initView() {
        String loginNickName = (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginNickName, "");
        tvName.setText(loginNickName);
        String headImg = (String) SharedPreferencesUtils.get(getActivity(), SharedPreferencesUtils.SpEnum.LoginHeadImg, "");
        HCUtils.loadWebImg(mContext, ivHeadImg, headImg);


    }

    @Override
    protected void initDates() {
        getData();

    }

    @Override
    protected void initListener() {

    }


    private void getData() {
        showLoadingDialog();
        new HttpRequest<LevelResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.agentMy)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<LevelResponse>() {
                    @Override
                    public void onSuccess(LevelResponse response) {
                        dissmissLoadingDialog();
                        model = response.model;
                        if (model == null) return;
                        setLevel(model.getLevel(), 50);

                        if (model.getConfig() == null || model.getConfig().size() == 0) return;
                        HashMap<String, LevelModel.ConfigBean> map = model.getConfig().get(0);
                        LevelModel.ConfigBean bean1 = map.get("10");
                        LevelModel.ConfigBean bean2 = map.get("20");
                        LevelModel.ConfigBean bean3 = map.get("30");

                        setText(tvStar2Hint, "升级新手团长后，" + "个人业绩达到" + bean1.getQuotas() + "元或累计收入达到" + bean1.getCash() + "元");
                        setText(tvStar3Hint, "升级铂金团长后，" + bean2.getReg_day() + "天内，" + "个人业绩达到" + bean2.getQuotas() + "元或累计收入达到" + bean2.getCash() + "元");
                        setText(tvStar4Hint, "升级钻石团长后，" + bean3.getReg_day() + "天内，" + "个人业绩达到" + bean3.getQuotas() + "元或累计收入达到" + bean3.getCash() + "元");
                        setText(tvStar2Reward, bean1.getReward());
                        setText(tvStar3Reward, bean2.getReward());
                        setText(tvStar4Reward, bean3.getReward());
                    }

                    @Override
                    public void onFail(String message, LevelResponse response) {
                        dissmissLoadingDialog();
                        HCUtils.loadFail(mContext, message);
                    }
                }).start(new LevelResponse());
    }


    private void setLevel(int level, int progress) {
        ivStar1.setImageResource(R.drawable.img_star_1_f);
        ivStar2.setImageResource(R.drawable.img_star_2_f);
        ivStar3.setImageResource(R.drawable.img_star_3_f);
        ivStar4.setImageResource(R.drawable.img_star_4_f);
        tvStar1.setTextColor(getResources().getColor(R.color.text_color9));
        tvStar2.setTextColor(getResources().getColor(R.color.text_color9));
        tvStar3.setTextColor(getResources().getColor(R.color.text_color9));
        tvStar4.setTextColor(getResources().getColor(R.color.text_color9));
        if (level == 1) {
            ivStar1.setImageResource(R.drawable.img_star_1);
            tvStar1.setTextColor(getResources().getColor(R.color.text_color3));
            progressBar = pb2;
        } else if (level == 10) {
            pb2.setProgress(100);
            ivStar2.setImageResource(R.drawable.img_star_2);
            tvStar2.setTextColor(getResources().getColor(R.color.text_color3));
            progressBar = pb3;
        } else if (level == 20) {
            pb2.setProgress(100);
            pb3.setProgress(100);
            ivStar3.setImageResource(R.drawable.img_star_3);
            tvStar3.setTextColor(getResources().getColor(R.color.text_color3));
            progressBar = pb4;
        } else if (level == 30) {
            pb2.setProgress(100);
            pb3.setProgress(100);
            pb4.setProgress(100);
            ivStar4.setImageResource(R.drawable.img_star_4);
            tvStar4.setTextColor(getResources().getColor(R.color.text_color3));
            progressBar = null;
        }
        if (progressBar != null) progressBar.setProgress(progress);
    }

}
